﻿#include "AsGLCoordPainter.h"


AsGLCoordPainter::AsGLCoordPainter()
	:m_texture(QOpenGLTexture::Target2D)
{
	initializeGL();
}

AsGLCoordPainter::~AsGLCoordPainter()
{
	clearup();
}

void AsGLCoordPainter::renderCoord()
{
	m_world.setToIdentity();
	m_camera.setToIdentity();
	m_camera.translate(m_transformParam.xTrans, -m_transformParam.yTrans, m_transformParam.zoomCoe);
	m_camera.rotate(m_transformParam.yRot, 0, 1, 0);
	m_camera.rotate(m_transformParam.xRot, 1, 0, 0);

	QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
	m_program->bind();
	m_program->setUniformValue(m_projLoc, m_transformParam.projMatrix);
	m_program->setUniformValue(m_mvLoc, m_camera * m_world);
	m_program->setUniformValue("ourTexture", 0);

	glDrawArrays(GL_TRIANGLES, 0, m_vertex.size() / 5);
}

void AsGLCoordPainter::initVertex()
{
	GLfloat i = 1;
	m_vertex = {
		-i,  i, -i,		0, 1,
		-i, -i, -i,		0, 0,
		 i, -i, -i,		1, 0,

		 i,  i, -i,		1, 1,
		-i,  i, -i,		0, 1,
		 i, -i, -i,		1, 0,

		-i,  i, -i,		1, 1,
		-i,  i,  i,		0, 1,
		-i, -i,  i,		0, 0,

		-i, -i, -i,		1, 0,
		-i,  i, -i,		1, 1,
		-i, -i,  i,		0, 0,

		-i, -i, -i,		0, 1,
		-i, -i,  i,		0, 0,
		 i, -i,  i,		1, 0,

		 i, -i, -i,		1, 1,
		-i, -i, -i,		0, 1,
		 i, -i,  i,		1, 0,

		-i, -i,  i,		0, 0,
		-i,  i,  i,		0, 1,
		 i,  i,  i,		1, 1,

		 i, -i,  i,		1, 0,
		-i, -i,  i,		0, 0,
		 i,  i,  i,		1, 1,

		 i, -i,  i,		0, 0,
		 i,  i,  i,		0, 1,
		 i,  i, -i,		1, 1,

		 i, -i, -i,		1, 0,
		 i, -i,  i,		0, 0,
		 i,  i, -i,		1, 1
	};
}

void AsGLCoordPainter::initTexture()
{
	m_texture.create();
	m_texture.setData(QImage("D:/gitLab/AiString/NewPCIDemo/jni/src/NewPCIDemoApplication/coordinate.png").mirrored());
	m_texture.setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);
	m_texture.setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);
	m_texture.setMinMagFilters(QOpenGLTexture::LinearMipMapLinear, QOpenGLTexture::Linear);
	m_texture.bind(0);
}

void AsGLCoordPainter::initializeGL()
{
	initializeOpenGLFunctions();
	m_program = new QOpenGLShaderProgram;
	bool flag = m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, "D:/gitLab/AiString/NewPCIDemo/jni/src/NewPCIDemoApplication/shader/CoordPainter.vert");
	flag = m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, "D:/gitLab/AiString/NewPCIDemo/jni/src/NewPCIDemoApplication/shader/CoordPainter.frag");
	flag = m_program->link();

	m_program->bind();
	m_projLoc = m_program->uniformLocation("projMatrix");
	m_mvLoc = m_program->uniformLocation("mvMatrix");

	m_vao.create();
	QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);

	m_vbo.create();
	m_vbo.bind();
	initTexture();
	initVertex();
	m_vbo.allocate(m_vertex.constData(), m_vertex.size() * sizeof(GLfloat));

	setupVertexAttribs();

	m_camera.setToIdentity();
	m_camera.translate(0, 0, -3);

	m_program->release();

	m_transformParam.modelMatrix.setToIdentity();
	m_transformParam.projMatrix.setToIdentity();
	m_transformParam.viewMatrix.setToIdentity();
}

void AsGLCoordPainter::setupVertexAttribs()
{
	m_vbo.bind();
	QOpenGLFunctions *functions = QOpenGLContext::currentContext()->functions();
	functions->glEnableVertexAttribArray(0);
	functions->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0);
	functions->glEnableVertexAttribArray(1);
	functions->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
	m_vbo.release();
}

void AsGLCoordPainter::clearup()
{
	if (m_program == nullptr)
	{
		return;
	}
	m_vbo.destroy();
	delete m_program;
	m_program = nullptr;
	m_texture.destroy();
}
